﻿import QtQuick 2.0

import "../../common"
import "../../../js/util.js" as Util
import "../../../js/UI.js" as Style

Page {
    id:m_changePassword_id
    width: parent.width
    height: parent.height
    property color col: Style.textCommonColor
    property var nobackDialog: null
    ATitle{
        id: m_titleID
        anchors.top: parent.top
        anchors.left: parent.left
        m_text: qsTr("修改密码")
        m_textcol:Style.fonttilteColor
        onBackSignal:subpageSignal(1,-1)
    }

    Column {
        anchors.left: parent.left
        anchors.leftMargin: m_lineValue*Util.leftMarginScale
        anchors.right: parent.right
        anchors.top: m_titleID.bottom
        anchors.bottom: parent.bottom

        LTextField {
            id: m_oldEdit_id
            width: parent.width
            height: m_lineValue
            font.pointSize: length>0?11:14
            placeholderText: qsTr("请输入原密码")
            echoMode: TextInput.Password

            SplitLine {
                anchors.bottom: parent.bottom
                width: parent.width
                anchors.left: parent.left
                anchors.leftMargin: 0.05*m_lineValue

            }
        }

        LTextField {
            id: m_firstEdit_id
            width: parent.width
            height: m_lineValue
            font.pointSize: length>0?11:14
            placeholderText: qsTr("请输入6~18位新密码")
            echoMode: TextInput.Password
            maximumLength: 18

            SplitLine {
                anchors.bottom: parent.bottom
                width: parent.width
                anchors.left: parent.left
                anchors.leftMargin: 0.05*m_lineValue
            }
        }

        LTextField {
            id: m_secondEdit_id
            width: parent.width
            height: m_lineValue
            font.pointSize: length>0?11:14
            placeholderText: qsTr("请再次输入新密码")
            echoMode: TextInput.Password
            maximumLength: 18

            SplitLine {
                anchors.bottom: parent.bottom
                width: parent.width
                anchors.left: parent.left
                anchors.leftMargin: 0.05*m_lineValue
            }
        }

        Item {
            width: parent.width
            height: m_lineValue*2
            PushButton {
                id: chgeBtn
                height: m_lineValue
                Accessible.focusable : true
                anchors.centerIn: parent
                enabled: m_oldEdit_id.text.length>0&&m_firstEdit_id.text.length>0&&m_secondEdit_id.text.length>0
                opacity: enabled?1:0.5
                radius: height*0.5
                anchors {
                    left: parent.left
                    right: parent.right
                    leftMargin: m_lineValue * Util.leftMarginScale * 2
                    rightMargin: m_lineValue * Util.rightMarginScale * 2
                }
                width: parent.width - Util.leftMarginScale * 2 * m_lineValue - Util.rightMarginScale * 2 * m_lineValue
                m_text: "完成"
                MouseArea {
                    id: mouseid
                    anchors.fill: parent
                    onClicked: {
                        if(chgeBtn.enabled)
                        {
                            m_oldEdit_id.focus = false
                            m_firstEdit_id.focus = false
                            m_secondEdit_id.focus = false
                            changePasswd(m_oldEdit_id.text,m_secondEdit_id.text)
                        }
                    }
                }
                activeFocusOnTab :true
                color: mouseid.pressed?Style.menuColor:Style.TitleColor
                //color: mouseid.pressed?"#718E27": Style.MainColor
            }

        }
    }
    function changePasswd(oldpwd,newpwd) {
        if(DatabaseEngine.persondata.pwd != m_oldEdit_id.text)
        {
            showWarningPage("原密码错误!")
            return
        }
        if (m_firstEdit_id.text == "" || m_secondEdit_id.text == "")
        {
            showWarningPage("密码不能为空!")
            return
        }
        if (m_firstEdit_id.text != m_secondEdit_id.text)
        {
            showWarningPage("两次输入密码不一致!")
            return
        }
        if (m_firstEdit_id.text.length < 6 || m_firstEdit_id.text.length > 18)
        {
            showWarningPage("失败,密码必须为6~18位有效密码!")
            return
        }
        if(m_oldEdit_id.text === m_firstEdit_id.text)
        {
            showWarningPage("新旧密码不能一样!")
            return
        }
        DatabaseEngine.setChangedPassword(m_firstEdit_id.text)
        Util.showPopWin(m_root_id,"qrc:/qml/form/personpage/ChangePasswdSuccess.qml",function (retValue){});
    }
    function showWarningPage(str)
    {
        Util.showPopWin(m_root_id,"qrc:/qml/form/dialog/Dialog_sure_and_cancel_warning.qml",str,function (retValue){});
    }
}
